/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scalia.meteocal.control;


import com.scalia.meteocal.entity.Event;
import com.scalia.meteocal.entity.Invitation;
import com.scalia.meteocal.entity.User;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author gabriele
 */
@Stateless
public class InvitationManager {

    @PersistenceContext
    EntityManager em;
    
    @EJB
    UserManager userManager;
    
    @EJB
    NotificationManager notificationManager;

    private void newInvitation(Invitation invitation) {
        invitation.setAccepted(false);
        em.persist(invitation);
    }
    
    public List<Invitation> getAllInvitations() {
        Query query = em.createNamedQuery(Invitation.findAll);
        return query.getResultList();
    }
    
    public void sendInvitationsTo(String[] usernames, Event event) {
        Date now = new Date();
        for (String username : usernames) {
            Invitation invitation = new Invitation();
            User user = em.find(User.class, username);
            invitation.setUser(user);
            invitation.setEvent(event);
            invitation.setCreationTimestamp(now);
            newInvitation(invitation);
            notificationManager.invitationsSent(event, user);
        }       
    }
    
    public List<Invitation> getInvitationsOfUser(User user) {
        Query query = em.createNamedQuery(Invitation.findAllInvitationsOfUser).setParameter("user", user);
        return query.getResultList();
    }

    public List<Invitation> getInvitationsOfEvent(Event event) {
        Query query = em.createNamedQuery(Invitation.findAllInvitationsOfEvent).setParameter("event", event);
        return query.getResultList();
    }

    public List<Invitation> getMyInvitations() {
        Query query = em.createNamedQuery(Invitation.findAllInvitationsOfUser).setParameter("user", userManager.getLoggedUser());
        return query.getResultList();
    }
    
    public void changeInvitation(Invitation invitation) {
        em.merge(invitation);
        em.flush();
    }
    
    public boolean isInvitedTo(User user, Event event) {
        Query query = em.createNamedQuery(Invitation.findAnInvitation).setParameter("user", user).setParameter("event", event);
        return query.getResultList().size() == 1;
    }
    
    public Invitation getInvitationOf(User user, Event event) {
        Query query = em.createNamedQuery(Invitation.findAnInvitation).setParameter("user", user).setParameter("event", event);
        if (query.getResultList().size() == 1)
            return (Invitation)query.getResultList().get(0);
        else return null;
    }
    
    public List<String> getNewPossibleInvitationsUsernames(List<String> currentInvitationsUsernames) {
        List<User> allUserExceptSelf = userManager.getAllUserExceptSelf();
        List<String>newPossibleInvitationsUsernames = new ArrayList<>();
        for (User u : allUserExceptSelf) {
            if (!currentInvitationsUsernames.contains(u.getUsername())) {
                newPossibleInvitationsUsernames.add(u.getUsername());
            }
        }
        return newPossibleInvitationsUsernames;
    }
}
